import os
from bs4 import BeautifulSoup as Soup
os.chdir(r"D:\JihongLeeLab\EPO_2020")

for year in range(1985, 2019):
	f = open(r"EPO{}.xml".format(year), "w", encoding="UTF-8")
	files = os.listdir(str(year))
	for file in files:
		appnum = file[:-4]
		rdir = str(year) + "\\" + file
		xml = open(rdir, "r").read()
		soup = Soup(xml, "xml")
		fault = soup.find("fault")
		if fault == None: 
			try:
				original_tag = soup.find("ops:world-patent-data")
				new_tag = soup.new_tag("appnum")
				original_tag.append(new_tag)
				new_tag.string = appnum
				xml = soup.prettify()
				xml = xml + "\n"
				f.write(xml)
			except:
				pass
			print(file)